package phone.qyg.com.qyg.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.application.QXApplication;
import com.app.base.tool.BaseActivity;
import com.app.config.AppHttpKey;
import com.app.config.ApplicationConfig;
import com.app.utils.CommonUtil;
import com.app.utils.JSONTool;
import com.app.utils.OptionImageUtils;
import com.app.utils.PromptManager;
import com.app.utils.TransformController;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.HashMap;
import java.util.Map;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.model.Delete;
import phone.qyg.com.qyg.model.MyVenue;
import phone.qyg.com.qyg.model.MyVenueDetail;
import phone.qyg.com.qyg.utils.AppHttpRequest;
import phone.qyg.com.qyg.utils.PopuDelete;

/**
 * 作者：lblbh on 2018/6/6 09:29
 * 邮箱：lanbuhan@163.com
 * 功能：我的场馆详情
 */
@ContentView(R.layout.my_venue_detail)
public class MyVenueDetailActivity extends BaseActivity {

    @ViewInject(R.id.back)
    TextView back;

    @ViewInject(R.id.item_title)
    TextView item_title;

    @ViewInject(R.id.item_venue_title)
    TextView item_venue_title;

    @ViewInject(R.id.item_address)
    TextView item_address;

    @ViewInject(R.id.venue_time)
    TextView venue_time;

    @ViewInject(R.id.venue_code)
    TextView venue_code;

    @ViewInject(R.id.venue_reserve_name)
    TextView venue_reserve_name;

    @ViewInject(R.id.venue_phone)
    TextView venue_phone;

    @ViewInject(R.id.venue_state)
    TextView venue_state;

    @ViewInject(R.id.venue_subitime)
    TextView venue_subitime;

    @ViewInject(R.id.quit_reserve)
    TextView quit_reserve;

    @ViewInject(R.id.item_img)
    ImageView item_img;

    @ViewInject(R.id.venue_rela)
    RelativeLayout venue_rela;

    MyVenueDetail model;

    int id;

    @Override
    public void initSucessView() {
        Object obj = getTransModels();

        if(obj instanceof MyVenue){
            MyVenue model = (MyVenue) obj;
            id = model.id;
        }
    }

    @Override
    public void initData(Bundle savedInstanceState) {

        mapKeys.put(AppHttpKey.ID,String.valueOf(id));
        AppHttpRequest.httpRequestDefalut(ApplicationConfig.VENUE_ORDER_DETAIL,
                mapKeys,
                new AppHttpRequest.OnAppHttpListener() {
                    @Override
                    public void onAppHttpState(boolean isState, String result) {
                        if(isState){
                            if(JSONTool.isStatus(result)){
                                model = JSONTool.jsonDefaluTranClazz(result,null, MyVenueDetail.class);
                                setData();
                            }else{
                                PromptManager.showToast(JSONTool.errorHint(result));
                            }
                        }else{
                            PromptManager.showToast(R.string.service_error);
                        }
                    }
                });
    }


    private void setData(){
        if(null == model)return;

        item_title.setText(setAttributeText(model.roomName));
        item_venue_title.setText(setAttributeText(model.venueName));
        venue_time.setText(setAttributeText(model.orderTime));
        venue_state.setText(setAttributeText(model.statusName));

        CommonUtil.setTextReplace(item_address,R.string.map_key_r,model.address);
        CommonUtil.setTextReplace(venue_code,R.string.order_num_r,model.orderCode);
        CommonUtil.setTextReplace(venue_reserve_name,R.string.venue_name_r,model.userName);
        CommonUtil.setTextReplace(venue_phone,R.string.venue_phone_r,model.phone);
        CommonUtil.setTextReplace(venue_subitime,R.string.submit_time_r,model.orderDate);

        venue_state.setTextColor(Color.parseColor(model.color));

        quit_reserve.setVisibility((model.isSign == 0)?View.VISIBLE:View.GONE);
        setImgPath(model.fixImgUrl,item_img, OptionImageUtils.getRectRatio43());
    }


    @Event(value = {R.id.back,R.id.quit_reserve,R.id.venue_rela})
    private void viewOnClick(View v){
        switch (v.getId()){
            case R.id.back:
                this.finish();
                break;
            case R.id.quit_reserve:
                PopuDelete.showPopuWinDeleteCourse(back,
                        new Delete(QXApplication.getContext().getResources().getString(R.string.quit_reserve),
                                model.roomName),
                        R.layout.popu_detele,
                        0,
                        new PopuDelete.DeleteState() {
                            @Override
                            public void state(boolean isSucess, int position) {
                                if(isSucess){
                                    Map<String,String> mapKeys = new HashMap<String,String>();
                                    mapKeys.put(AppHttpKey.ID,String.valueOf(model.id));

                                    AppHttpRequest.appQuitState(ApplicationConfig.CANCEL_VENUE,
                                            mapKeys,
                                            new AppHttpRequest.OnAppHttpListener() {
                                                @Override
                                                public void onAppHttpState(boolean isState, String result) {
                                                    if(isState){
                                                        PromptManager.showToast(R.string.quit_reserve_sucess);
                                                        initData(null);
                                                    }
                                                }
                                            });

                                }
                            }
                        });
                break;
            case R.id.venue_rela:
                TransformController.transformControllerModel(QXApplication.getContext(),VenuesDetailActivity.class,model);
                break;
        }
    }

    @Override
    public void initViewHandler() { }
}
